package com.smit.livevideo.sample.util;

import java.util.List;

import com.smit.livevideo.sample.CommonData;
import com.smit.livevideo.sample.dao.ChannelDAO;
import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/12/7.
 * LivevideoActivity的工具类，主要有切台控制函数
 */
public final class LivevideoUtil
{
    private static final String TAG = LivevideoUtil.class.getSimpleName();

    private LivevideoUtil()
    {

    }

    /**
     * 播放最后一次播放的节目
     * @return boolean
     */
    public static boolean playLastServices()
    {
        LogUtil.trace(TAG, "playLastServices~~~");
        int last = ShareValueUtil.getShareIntValue(CommonData.PREF_LAST_PLAY_CHANNEL_INDEX, 0);
        if (last >= 0 && last < getChannelListSize())
        {
            playServiceByIndex(last);
        }
        else
        {
            playServiceByIndex(0);
            ShareValueUtil.putShareIntValue(CommonData.PREF_LAST_PLAY_CHANNEL_INDEX, 0);
        }
        LogUtil.error(TAG, "playLastServices last=" + last);
        return false;
    }

    /**
     * 播放上一个节目
     * @return
     */
    public static void playServicesForward()
    {
        LogUtil.trace(TAG, "playServicesForward~~~");
        int last = ShareValueUtil.getShareIntValue(CommonData.PREF_LAST_PLAY_CHANNEL_INDEX, 0);
        List<Integer> list = ChannelDAO.getInstance().getCurrentChannelIndexList();
        LogUtil.trace(TAG, "playServicesForward~~~" + list.size());
        if (list != null && list.size() > 0)
        {
            if (last == 0)
            {
                int index = list.get(list.size() - 1);
                playServiceByIndex(index);
            }
            else if (last > 0)
            {
                int index = list.get(last - 1);
                playServiceByIndex(index);
            }
            else if (last < 0)
            {
                playServiceByIndex(0);
            }
        }
    }

    /**
     * 播放下一个节目
     * @return
     */
    public static void playServicesRewind()
    {
        LogUtil.trace(TAG, "playServicesRewind~~~");
        int last = ShareValueUtil.getShareIntValue(CommonData.PREF_LAST_PLAY_CHANNEL_INDEX, 0);
        List<Integer> list = ChannelDAO.getInstance().getCurrentChannelIndexList();
        LogUtil.trace(TAG, "playServicesRewind~~~" + list.size());
        if (list != null && list.size() > 0)
        {
            if (last == list.size() - 1)
            {
                int index = list.get(0);
                playServiceByIndex(index);
            }
            else if (last >= 0)
            {
                int index = list.get(last + 1);
                playServiceByIndex(index);
            }
            else if (last < 0)
            {
                playServiceByIndex(0);
            }
        }
    }

    /**
     * 获取所有频道的size
     * @return
     */
    private static int getChannelListSize()
    {
        List<String> list = ChannelDAO.getInstance().getCurrentChannelNameList();
        if (list != null)
        {
            return list.size();
        }
        return 0;
    }

    /**
     * 播放指定频道
     * @param index
     */
    private static void playServiceByIndex(int index)
    {
        LogUtil.trace(TAG, "playServiceByIndex : " + index);
        /**这里默认不能用0，因为第一次播放是0，下面的判断会失效*/
        int pre = ShareValueUtil.getShareIntValue(CommonData.PREF_LAST_PLAY_CHANNEL_INDEX, -1);
        if (pre != index)
        { //如果再次选择播放当前，不需要调整参数，
            ShareValueUtil.putShareIntValue(CommonData.PREF_LAST_PLAY_CHANNEL_INDEX_BAK,
                    (pre == -1 ? 0 : pre)); //前一个是空，这里默认保存0
            ShareValueUtil.putShareIntValue(CommonData.PREF_LAST_PLAY_CHANNEL_INDEX, index);
        }
        SDKAPIUtil.getServiceControl().playServiceByIndex(index);
    }
}
